package com.example.sxproject.controller;

import com.example.sxproject.pojo.*;
import com.example.sxproject.service.CardService;
import com.example.sxproject.utils.JwtUtil;
import com.example.sxproject.utils.Md5Util;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/card")
public class CardController {

    @Autowired
    private CardService cardService;

    //通过userId找到用户所有卡的信息
    @GetMapping("/cards")
    public Result getCardsByUserId(@RequestParam Integer userId) {
        List<Card> cards= cardService.getCardsByUserId(userId);
        return Result.success(cards);
    }

    //添加：添加一个银行卡
    @PostMapping("/insertcard")
    public String addCard(Card card, HttpSession session, String password, String remPassword) {
        card.setIdNumber(" ");
        if(password == null || password.equals("") || remPassword == null || remPassword.equals("")) {
            System.out.println("密码为空");
            session.setAttribute("msg", "密码不能为空!");
            return "credit_card_application";
        }

        if(password.equals(remPassword)) {
            //支付时间，获取当前时间
            LocalDateTime dateTime = LocalDateTime.now();
            String time = dateTime.toString(); // 转为string类型

            LocalDateTime dateTimePlusOneMonth = dateTime.plusMonths(1); //加一个月
            String repayTime = dateTimePlusOneMonth.toString(); //转换成字符串

            time = time.substring(0, 19);
            time = time.replace('T', ' ');

            repayTime = repayTime.substring(0, 19);
            repayTime = repayTime.replace('T', ' ');

            card.setBeginDate(time);
            card.setRepayDate(repayTime);
            card.setRepaid(card.getTotal());
            cardService.addCard(card);
            return "success";
        }
        else {
            session.setAttribute("msg", "两次输入的密码不一致!");
            return "credit_card_application";
        }


    }

    //更新：更新全部
    @PutMapping("/updateall")
    public Result updateCard(@RequestBody Card card) {
        cardService.updateAll(card);
        return Result.success();
    }

    //查询：验证银行卡密码
    @GetMapping("/verifypassword")
    public String verifyPassword(@RequestParam String idNumber, @RequestParam String enterpassword) {
        boolean verify = cardService.verifyPassword(idNumber, enterpassword);
        if (verify) { return "密码正确"; }
        else { return "密码错误"; }
    }


    //更新：更改指定银行卡的密码
    @PutMapping("/updatepassword")
    public Result updatePassword(@RequestParam String idNumber, @RequestParam String password) {
        cardService.updatePassword(idNumber, password);
        return Result.success();
    }

    //更新：更改指定银行卡的余额
    @PutMapping("/updatebalance")
    public Result updateBalance(@RequestParam String idNumber, @RequestParam double balance) {
        cardService.updateBalance(idNumber, balance);
        return Result.success();
    }

    //更新：实现假删除 + 冻结
    @RequestMapping("/delectcard")
    public String deleteCard(String cardId, Integer statusId, String password, HttpSession session) {
        User user = (User) session.getAttribute("user");
        boolean verify = cardService.verifyPassword(cardId, password);
        if (verify) { //密码正确
            cardService.deleteCard(cardId, statusId); // 修改
            List<Card> cards= cardService.getCardsByUserId(user.getId());
            session.setAttribute("cards", cards);
            return "card";
        }
        else {
            session.setAttribute("msg", "密码错误!");
            return "cardSet";
        }
    }

    //查询：根据银行卡号查询数据
    @GetMapping("findcard")
    public Result findByIdNumber(@RequestParam String idNumber){
        Card card = cardService.findByIdNumber(idNumber);
        return Result.success(card);
    }

    //根据卡号修改所属用户
    @RequestMapping("updateuserId")
    public String updatestatusId(String idNumber, String password, HttpSession session){

        User user = (User) session.getAttribute("user");

        boolean verify = cardService.verifyPassword(idNumber, password);

        if (verify) { //密码正确
            cardService.updatestatusId(idNumber, user.getId());
            List<Card> cards= cardService.getCardsByUserId(user.getId());
            session.setAttribute("cards", cards);

            return "card";
        }
        else {
            session.setAttribute("msg", "密码错误!");
            return "card";
        }
    }



}
